import 'package:bot_toast/bot_toast.dart';
import 'package:easy_read_comic/internal/api/comic_api.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class ComicLookForController extends GetxController {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  TextEditingController titleController = TextEditingController();

  TextEditingController linkController = TextEditingController();

  void submit() {
    var cancel = BotToast.showLoading();
    try {
      if (this.formKey.currentState!.validate()) {
        this.formKey.currentState!.save();
        ComicApi()
            .comicLookFor(
          titleController.value.text,
          link: linkController.value.text,
        )
            .then(
          (response) {
            BotToast.showText(text: response.data["msg"]);
            if (response.data["code"] == 1) {
              Get.back();
              this.formKey.currentState?.reset();
            }
          },
        );
      }
    } catch (e) {
      BotToast.showText(text: "系统错误");
    } finally {
      Future.delayed(Duration(microseconds: 500), () {
        cancel();
      });
    }
  }
}
